package AutoTest2018.utils.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

import static AutoTest2018.utils.base.SSLHelper.createSSLClientDefault;

public class HttpsClientUtil
{
    /***
     * https的post请求，请求体是json格式的
     * @param url
     * @param jsonObject
     * @return
     */
    public String httpsPostJson(String url, JSONObject jsonObject) throws IOException
    {
        CloseableHttpClient hp = createSSLClientDefault();
        HttpPost post = new HttpPost(url);
        //设置请求头
        post.setHeader("Content-Type", "application/json");
        String body=jsonObject.toJSONString();
        //设置请求体
        post.setEntity(new StringEntity(body));
        //执行请求
        CloseableHttpResponse response = hp.execute(post);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity,"utf-8");
        System.out.println(content);
        hp.close();
        return content;
    }
}
